#!/usr/bin/env bash
# installation script for note - must be ran as root
# Copyright ©2013-2023 Jason Trunks
# https://notabug.org/JasKinasis/note

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

# NOTE: This script is still very much a WIP!
# I'm still working out what I want to do with it.

# TODO: The backup and remove option does not work when the uninstall is performed as root.
# Instead - it tries to remove the root users personal notes.
# So we need to find each users notes, back them up and then remove all of them.
# In other words - search home for folders called .note (maxdepth of 2)
# If found - tar.gz them and copy them to the appropriate users /home/

# Installation paths
INSTALLDIR="/usr/local/bin"
MANDIR="/usr/local/share/man"
COMPLETIONDIR="/etc/bash_completion.d"
USERDATADIR=$HOME/.note/

function _areYouSure()
{
	local USEROPTION=
	echo $1
	while :
	do 
		read -r -p "Please press y/n : " -n 1 USEROPTION

		case ${USEROPTION} in 
			y|Y)
				return 0
				;;
			n|N)
				return 1
				;;
			*) 
		esac
		echo
	done
}

function _removeUserData()
{
	local USEROPTION=
	echo;echo
	echo "What do you want to do with your notes and settings?"
	echo "	- Backup and delete (b)"
	echo "	- Delete (d)"
	echo "	- Keep (k)"
	echo "	- Quit (q) - quit the uninstaller"
	echo "	- Cancel (c) - cancel the uninstaller" 
	while :
    do	
		read -r -p "Please press b, d, k, q or c  : " -n 1 USEROPTION

		case $USEROPTION in
			b|B)
				echo;echo
			    echo "Backing up notes:"
				note --backup
				echo "Removing users notes and settings:"
				rm ${USERDATADIR} -rf
				return
				;;
			d|D)
				echo;echo
				echo "Removing .note directory:"
				rm ${USERDATADIR} -rf
				return
				;;
			k|K)
				echo;echo
				echo "Keeping users notes and settings"
			   return;
			   ;;
			q|Q|c|C)
				return 1;
				;;
			*)
				echo;
				echo "ERROR: Invalid option selected, please try again!"
		esac
		echo
    done	
}

echo "Note uninstaller V0.1"
echo "©2023 Jason Trunks"
echo "Blah blah blah licency bollocks... etc"
echo
echo
echo "This script will completely uninstall all components of note"

_areYouSure "Do you wish to continue?" 
if [[ $? -eq 0 ]]; then
	_removeUserData
	if [[ $? -eq 0 ]]; then
		echo "Removing note and notesettings"
		rm ${INSTALLDIR}/note ${INSTALLDIR}/notesettings
		echo "Removing documentation"
		rm ${MANDIR}/man1/note.1 ${MANDIR}/man1/notesettings.1
		echo "Removing bash_completion file"
		rm ${COMPLETIONDIR}/note
		echo
		echo "Uninstall complete!"
		echo;echo
		exit
	fi
fi

echo;echo
echo "Uninstall cancelled - no changes have been made to your system!"
echo;echo
exit

